import * as Cesium from 'cesium'
import { onUnmounted } from 'vue'
import { BillboardManager } from '../utils/BillboardManager'

export function useBillboardClick(billboardManager: BillboardManager) {
  const viewer = billboardManager.viewer
  const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)

  const handleClick = (movement: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
    const picked = viewer.scene.pick(movement.position)
    if (picked && picked.primitive instanceof Cesium.Billboard) {
      const result = billboardManager.findBillboard(picked.primitive)
      if (result) {
        console.log(result, '点击了')
      }
    }
  }

  handler.setInputAction(handleClick, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  handler.setInputAction(handleClick, Cesium.ScreenSpaceEventType.RIGHT_CLICK)

  const removeBillboardClick = () => {
    handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }

  onUnmounted(() => {
    removeBillboardClick()
  })

  return {
    removeBillboardClick,
  }
}
